package com.hex.ds.hdtp.core.app.script.test.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.hex.ds.hdtp.core.app.common.page.PageData;
import com.hex.ds.hdtp.core.app.common.page.converter.IPageAndPageDtoConverter;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.test.converter.IScriptDataTestResultConverter;
import com.hex.ds.hdtp.core.app.script.test.dto.request.ScriptDataTestResultRequestDto;
import com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestResultResponseDto;
import com.hex.ds.hdtp.core.app.script.test.service.IScriptDataTestResultService;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.script.test.po.ScriptDataTestResultPo;
import com.hex.ds.hdtp.core.inf.script.test.repository.IScriptDataTestResultRepository;
import com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertTaskPo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.app.script.test.service.impl
 * @ClassName ScriptDataTestResultService
 * @Description: 带数据测试脚本结果-业务接口实现
 * @Author: wz.li
 * @Date 2023/8/7 12:06
 * @Version v2.0.2
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptDataTestResultService implements IScriptDataTestResultService {

    private final IScriptDataTestResultRepository scriptDataTestResultRepository;

    private final IScriptDataTestResultConverter converter;

    private final IPageAndPageDtoConverter pageAndPageDtoConverter;

    @Value("${script.convert.relativePath}")
    private String relativePath;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptDataTestResultRequestDto, pageDto] <br>
     * @Return: com.hex.ds.hdtp.core.app.common.page.PageData <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 12:06 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public PageData queryListByPage(ScriptDataTestResultRequestDto scriptDataTestResultRequestDto, PageDto pageDto) {
        final Page<ScriptDataTestResultPo> pageResult = scriptDataTestResultRepository.queryListByPage(
                converter.toScriptDataTestResultPo(scriptDataTestResultRequestDto), pageAndPageDtoConverter.toPage(pageDto));
        List<ScriptDataTestResultResponseDto> responseDtos = converter.toScriptDataTestResultResponseDtoList(pageResult.getData());
        return new PageData(responseDtos, pageResult.getTotal());
    }

    /*
     * @Method: queryList <br>
     * @Param: [batchCode, taskPkId] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.app.script.test.dto.response.ScriptDataTestResultResponseDto> <br>
     * @Description：不分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 15:15 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptDataTestResultResponseDto> queryList(String batchCode, String taskPkId) {
        return converter.toScriptDataTestResultResponseDtoList(scriptDataTestResultRepository.queryList(batchCode, taskPkId));
    }

    /*
     * @Method: add <br>
     * @Param: [scriptDataTestResultRequestDto] <br>
     * @Return: void <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 12:06 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void add(ScriptDataTestResultRequestDto scriptDataTestResultRequestDto) {
        int count = scriptDataTestResultRepository.add(converter.toScriptDataTestResultPo(scriptDataTestResultRequestDto));
        if (count != 1) {
            throw new RuntimeException("脚本测试结果-新增失败");
        }
    }

    /*
     * @Method: modifyById <br>
     * @Param: [scriptDataTestResultRequestDto] <br>
     * @Return: void <br>
     * @Description：根据主键更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 12:06 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void modifyById(ScriptDataTestResultRequestDto scriptDataTestResultRequestDto) {
        int count = scriptDataTestResultRepository.modifyById(converter.toScriptDataTestResultPo(scriptDataTestResultRequestDto));
        if (count != 1) {
            throw new RuntimeException("脚本测试结果-更新失败");
        }
    }

    /*
     * @Method: removeById <br>
     * @Param: [pkId] <br>
     * @Return: void <br>
     * @Description：根据主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 12:06 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public void removeById(String pkId) {
        int count = scriptDataTestResultRepository.removeById(pkId);
        if (count != 1) {
            throw new RuntimeException("脚本测试结果-删除失败");
        }
    }

    /**
     * @Method downloadConvertFile
     * @param scriptDataTestResultRequestDto
     * @Return java.lang.String
     * @Description 下载转换后脚本文件
     * @Author Yin.Yang
     * @Date 2023/10/20 15:13
     * @Version V1.0
     */
    @Override
    public String downloadScriptFile(ScriptDataTestResultRequestDto scriptDataTestResultRequestDto) {
        // 获取下载文件路径、转换文件路径、转换文件路径
        String downloadFileRelativePath = getTmpFilePath("download-file");
        String convertFilSavePath = downloadFileRelativePath + File.separator + "convertFile";
        // 将转换后文件复制到下载目录下
        FileUtil.copyContent(new File(scriptDataTestResultRequestDto.getScriptPath()), new File(convertFilSavePath), true);
        //zip压缩下载文件
        ZipUtil.zip(downloadFileRelativePath);
        return downloadFileRelativePath + ".zip";
    }

    /**
     * @Method getTmpFilePath
     * @param fileType
     * @Return java.lang.String
     * @Description 获取临时路径
     * @Author Yin.Yang
     * @Date 2023/10/20 15:19
     * @Version V1.0
     */
    private String getTmpFilePath(String fileType) {
        String filePath = StrUtil.format("{}/tmp/{}/{}/{}", relativePath, DateUtil.today(), fileType, System.currentTimeMillis());
        FileUtil.mkdir(filePath);
        return filePath;
    }

}
